All files / web/src/app/api/arcade/rooms/[roomId]/report route.ts

0% Statements 0/94
0% Branches 0/1
0% Functions 0/1
0% Lines 0/94

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95                                                                                                                                                                                             
import { NextResponse } from 'next/server'
import { createReport } from '@/lib/arcade/room-moderation'
import { getRoomMembers } from '@/lib/arcade/room-membership'
import { withAuth } from '@/lib/auth/withAuth'
import { getUserId } from '@/lib/viewer'
import { getSocketIO } from '@/lib/socket-io'

/**
 * POST /api/arcade/rooms/:roomId/report
 * Submit a report about another player
 * Body:
 *   - reportedUserId: string
 *   - reason: string (enum)
 *   - details?: string (optional)
 */
export const POST = withAuth(async (request, { params }) => {
  try {
    const { roomId } = (await params) as { roomId: string }
    const userId = await getUserId()
    const body = await request.json()

    // Validate required fields
    if (!body.reportedUserId || !body.reason) {
      return NextResponse.json(
        { error: 'Missing required fields: reportedUserId, reason' },
        { status: 400 }
      )
    }

    // Validate reason
    const validReasons = ['harassment', 'cheating', 'inappropriate-name', 'spam', 'afk', 'other']
    if (!validReasons.includes(body.reason)) {
      return NextResponse.json({ error: 'Invalid reason' }, { status: 400 })
    }

    // Can't report yourself
    if (body.reportedUserId === userId) {
      return NextResponse.json({ error: 'Cannot report yourself' }, { status: 400 })
    }

    // Get room members to verify both users are in the room and get names
    const members = await getRoomMembers(roomId)
    const reporter = members.find((m) => m.userId === userId)
    const reported = members.find((m) => m.userId === body.reportedUserId)

    if (!reporter) {
      return NextResponse.json({ error: 'You are not in this room' }, { status: 403 })
    }

    if (!reported) {
      return NextResponse.json({ error: 'Reported user is not in this room' }, { status: 404 })
    }

    // Create report
    const report = await createReport({
      roomId,
      reporterId: userId,
      reporterName: reporter.displayName,
      reportedUserId: body.reportedUserId,
      reportedUserName: reported.displayName,
      reason: body.reason,
      details: body.details,
    })

    // Notify host via socket (find the host)
    const host = members.find((m) => m.isCreator)
    if (host) {
      const io = await getSocketIO()
      if (io) {
        try {
          // Send notification only to the host
          io.to(`user:${host.userId}`).emit('report-submitted', {
            roomId,
            report: {
              id: report.id,
              reporterName: report.reporterName,
              reportedUserName: report.reportedUserName,
              reportedUserId: report.reportedUserId,
              reason: report.reason,
              createdAt: report.createdAt,
            },
          })
        } catch (socketError) {
          console.error('[Report API] Failed to notify host:', socketError)
        }
      }
    }

    return NextResponse.json({ success: true, report }, { status: 201 })
  } catch (error: any) {
    console.error('Failed to submit report:', error)
    return NextResponse.json({ error: 'Failed to submit report' }, { status: 500 })
  }
})